<template>
  <div class="app-container">
    <el-card class="pt10">
      <el-row :gutter="10" class="mb8" style=" display: flex;justify-content: flex-end;">
        <el-col :span="1.5" >
          <el-button
            type="primary"
            circle
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          ></el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"

            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            circle
            v-hasPermi="['system:config:edit']"
          ></el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"

            circle
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:config:edit']"
          ></el-button>
        </el-col>
      </el-row>
      <div class="head-container">
        <el-input
          v-model="resourceName"
          placeholder="请输入活动名称"
          clearable
          size="small"
          prefix-icon="el-icon-search"
          style="margin-bottom: 20px"
        />
      </div>
      <el-scrollbar wrap-class="scrollbar-wrapper" style="min-height: 65vh;">
        <div class="head-container">
          <el-tree
            :data="resourceOptions"
            :props="{children: 'children', label: 'activityName'}"
            :expand-on-click-node="false"
            default-expand-all
            :filter-node-method="filterNode"
            ref="tree"
            @node-click="handleNodeClick"
          >
             <span class="custom-tree-node" slot-scope="{ node, data }">
              <span >{{ node.label }}</span>
            </span>
          </el-tree>
        </div>
      </el-scrollbar>
    </el-card>

    <!-- 添加或修改回单环节配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="10em" class="input-element">
        <el-form-item label="活动名称" prop="activityName">
          <el-input v-model="form.activityName" placeholder="请输入活动名称" maxlength="30" show-word-limit/>
        </el-form-item>
        <el-form-item label="活动编码" prop="activityCode">
          <el-input v-model="form.activityCode" placeholder="请输入活动编码" maxlength="30" show-word-limit/>
        </el-form-item>
        <el-form-item label="活动开始时间" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择活动开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择活动结束时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listActivity, getActivity, addActivity, updateActivity, delActivity
} from '@/api/mx/rebate'

export default {
  name: "activityTree",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 回单环节配置表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityName: null,
        activityCode: null,
        startTime: null,
        endTime: null,
        orderNumber: null,

      },
      resourceOptions:[],

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        activityName: [
          { required: true, message: "活动名称不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始日期不能为空", trigger: "blur" }
        ] ,
        endTime: [
          { required: true, message: "结束日期不能为空", trigger: "blur" }
        ]
      },
      resourceName: undefined,//搜索名称
      modify: false,//是否可以修改 -false 可以修改 true不能修改
    };
  },
  watch: {
    // 根据名称筛选
    resourceName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getInit();
  },
  methods: {
    /** 查询回单环节配置列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.configList = response.rows;
        this.resourceOptions = [{
          activityName: "活动列表",
          activityCode: "-1",
          children: this.configList
        }]
        this.loading = false;
      });
    },
    getInit() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.configList = response.rows;
        this.resourceOptions = [{
          activityName: "活动列表",
          activityCode: "-1",
          children: this.configList
        }]
        console.log(this.configList)
        if (this.configList.length > 0) {
          this.handleNodeClick(this.configList[0])
        }
        this.loading = false;
      });

    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        linkName: null,
        linkStatus: "1",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 树选中后逻辑
    handleSelectionChange(selection) {
      this.ids = selection.id
      this.single = selection.id === '-1'
      this.multiple = selection.id === '-1'
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.modify = false;
      this.title = "添加回单环节配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivity(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.modify = true;
        this.title = "修改回单环节配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.title !== '添加回单环节配置') {
            updateActivity(this.form).then(response => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivity(this.form).then(response => {
              if (response.code == 200) {
                this.$message.success("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除回单环节配置编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delActivity(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.linkName.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.$emit('getFiledList',data.startTime,data.endTime)
      this.handleSelectionChange(data)

    },
  }
};
</script>

<style scoped>
.input-element {
  width: 90%; /* 设置输入框宽度为80% */
}

</style>
